//用于加解密
//@ts-ignore
import JSEncrypt from "jsencrypt";
const CryptoJS = require("crypto-js");
const key = "123456789abcdef0";
const iv = "abcdefghijkasdaaa";

const publicKey =
  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrECKvoqfM2erJkPoQdMvXcTejPu8n52xxY+2MuSqN00FfTp7mhD9JgdVy5UalmInzcO+IkOuT8Ysw1SvgA3UfczX9f2G4QSEyGw5e/jimu99YxRXxDTO6cB5GVG6DSmxwfCxeZ2ao3VO/pdx2V13t+y/SJNDkxg3dNT1HSLYVMQIDAQAB";

const encrypt = new JSEncrypt({});
encrypt.setPublicKey(publicKey);

const finalKey = CryptoJS.enc.Utf8.parse(key);
const finalIv = CryptoJS.enc.Utf8.parse(iv);

//加密
function getSecretString(data: string): string {
  return CryptoJS.AES.encrypt(data, finalKey, {
    iv: finalIv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  }).toString();
}

//解密
function getOriginString(data: string): any {
  if (data == null) {
    return null;
  }
  // let finalKey = CryptoJS.enc.Utf8.parse(key);
  // let finalIv = CryptoJS.enc.Utf8.parse(iv);
  // let secret = CryptoJS.enc.Hex.parse(data);

  const bytes = CryptoJS.AES.decrypt(data, finalKey, {
    iv: finalIv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  // console.log(CryptoJS.enc.Utf8.stringify(bytes));
  // console.log(bytes.toString(CryptoJS.enc.Utf8));
  return CryptoJS.enc.Utf8.stringify(bytes);
}

//rsa加密
function getRsaSecretString(data: string) {
  // console.log(JSEncrypt)
  // console.log(data);

  // debugger;
  const info = encrypt.encrypt(data);
  return info;
}

//rsa解密
function getRsaOriginString(data: string) {
  // const encrypt = new JSEncrypt();
  // encrypt.setPublicKey();
  return encrypt.decrypt(data);
}

export {
  getOriginString,
  getSecretString,
  getRsaSecretString,
  getRsaOriginString,
};
